iT邦幫忙

2023 iThome 鐵人賽

DAY 11
0

昨天簡單介紹一下 RDS

相信對於 RDS 的基本操作大家應該不陌生

而今天就會以如何在 RDS PostgreSQL 實作資料庫稽核

讓大家更了解 RDS 資料庫的使用

無論是 aurora postgres 相容版本或是 posrgresql 資料庫都參考同樣設定

前提條件

確定資料庫叢集或實體是否將 Log 輸出至 CloudWatch Log

https://ithelp.ithome.com.tw/upload/images/20230926/20141518otRsLZlWfq.png

建立 db role

登入到資料庫中
並執行下列指令

CREATE ROLE rds_pgaudit;

修改參數群組

新增一個參數 shared_preload_libraries 值為 pgaudit
修改 pgaudit.role 這個參數的值為 rds_pgaudit

https://ithelp.ithome.com.tw/upload/images/20230926/201415182ETneLeWFZ.png

如果是使用 default 的參數群組可能無法修改值,需要另外建新的

重啟資料庫

有使用參數群組的資料庫修改完後要記得重啟

確認稽核設定

輸入下方的 sql 語法確認是否有出現 pgaudit 的字樣

show shared_preload_libraries;

建立擴充套件

輸入下列語法以便建立 pgaudit 的套件

CREATE EXTENSION pgaudit;

確定稽核角色設定

確定參數群組設定的稽核角色 pgaudit.role 是否有正確的載入 rds_pgaudit

show pgaudit.role;

設定 pgaudit.log 參數

資料庫在稽核現在使用狀況時會將 Log 寫到 pgaudit.log 中

而我們需要設定稽核層級

層級設定為何則需要按需決定

共有下列幾種

不確定該選甚麼建議可以先選擇 DDL

  • ALL audits the following commands.
  • MISC audits miscellaneous commands, such as DISCARD, FETCH, CHECKPOINT, VACUUM, SET.
  • DDL audits all data description language (DDL) that is not included in the ROLE class.
  • ROLE audits statements related to roles and privileges, such as GRANT, REVOKE, CREATE/ALTER/DROP ROLE.
  • FUNCTION audits function calls and DO blocks.
  • WRITE audits INSERT, UPDATE, DELETE, TRUNCATE, and COPY when the destination is a relation.
  • READ audits SELECT and COPY when the source is a relation or a query.

檢查 pgaudit.log 設定

登入資料庫後輸入下列語法確認目前 log 參數設定狀態

show pgaudit.log;

設定 audit 參數

將 test_database 替換成自己的資料庫名稱

ALTER DATABASE test_database set pgaudit.log='DDL';

Create Table 或 Alter Table 並檢查 Log

因為是設定 DDL 的稽核

因此需要實際修改資料庫結構

才有辦法抓到相關的運作是否可正常執行

檢查 CloudWatch Log /aws/rds/cluster/資料庫名稱/postgresql

更多稽核

如果打算設定不同角色

以便做不同稽核

可以詳閱參考資料建立多個角色個別指定不同的參數

監控

在 CloudWatch Log 中的 Metrics Filter

可以針對 Log 建立篩選反向建立指標

最後針對指標去做監控和告警

參考資料:

  1. How do I use the pgaudit extension to audit my Amazon RDS DB instance that is running PostgreSQL?

上一篇
Day 10 介紹 RDS
下一篇
Day 12 RDS bak 備份還原
系列文
上完 AWS 線上課程就打算應徵相關工作是否搞錯甚麼 共 30 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言